//
//  MBSpaceAddMusicDetailViewController.m
//  MiBaoTu
//
//  Created by admin on 16/9/22.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceAddMusicDetailViewController.h"
#import "MBLoginController.h"

#import "MBSpaceCollectionMusicCell.h"
#import "CollectionShowView.h"

@interface MBSpaceAddMusicDetailViewController ()<UITableViewDataSource,UITableViewDelegate> {
    UITableView                     *_tableView;
    CollectionShowView              *showView;
}

@end

@implementation MBSpaceAddMusicDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavBar];
    
    [self onCreateUI];
}

#pragma mark - Actions
#pragma mark - UI
- (void)setNavBar
{
    [self setTitleView:_titleString];
    [self setBackItem];
    
    UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
    bt.frame = CGRectMake(0, 0, 60, 30);
    [bt setTitle:@"关闭" forState:UIControlStateNormal];
    [bt setTitle:@"关闭" forState:UIControlStateHighlighted];
    bt.titleLabel.font = [UIFont systemFontOfSize:13.];
    [bt setTitleEdgeInsets:UIEdgeInsetsMake(0, 20, 0, 0)];
    [bt addTarget:self action:@selector(onSelectItemForBack) forControlEvents:UIControlEventTouchUpInside];
    [self setRightBarItems:bt];
}

- (void)onCreateUI
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.backgroundView = nil;
    [self.view addSubview:_tableView];

}


- (void)onSelectItemForBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)onUpdateUIForView:(UIDeviceOrientation)orientation
{
    _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.view.height);
    showView.orientation = self.orientation;
    [_tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellID = @"Collection_Music_Cell";
    MBSpaceCollectionMusicCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[MBSpaceCollectionMusicCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    }
    //是否隐藏更多按钮
    cell.isHideMoewBt = YES;
//    [cell onCompileCellForLayout:self.isCompiling WithAllSelct:self.isAllSelcting];
    
    cell.orientation = self.orientation;

    if (_dataArr.count > indexPath.row) {
        cell.musicDic = [_dataArr objectAtIndex:indexPath.row];
        cell.isShowDowned = YES;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100*PERCEN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"*******添加歌曲到收藏");
    
    UserModel *model = [UserManager standerDefult].userModel;
    if (_dataArr.count > indexPath.row) {
        if (!model.UserID) {
            MBLoginController *registerLastVC = [[MBLoginController alloc] init];
            registerLastVC.formBaseViewCtrl = NSStringFromClass([self class]);
            [self.navigationController pushViewController:registerLastVC animated:YES];
            return;
        }
        
        NSDictionary *dic = [_dataArr objectAtIndex:indexPath.row];
        NSString *fileId = [dic objectForKey:@"fileID"];
        
        if (self.type == MBSpaceAddMusicType_SetAlbum) {
            
            if (self.cateID.length == 0) return;
            
            NSString *url = [NSString stringWithFormat:SPACE_MOVETO_URL_PATH,model.UserID,fileId,@1,self.cateID];
            if ([dic.allKeys containsObject:@"title"]) {
                //是亲子秀
                url = [NSString stringWithFormat:SPACE_MOVETO_URL_PATH,model.UserID,[dic objectForKey:@"musicId"],@2,self.cateID];
            }
            [[HTTPManager standerDefault]AFGetRequest:url view:nil success:^(NSDictionary *json) {
                
                if ([json isKindOfClass:[NSDictionary class]]) {
                    if ([json[@"ret_code"] integerValue]==0) {

                        if (showView) {
                            [showView removeFromSuperview];
                            showView = nil;
                        }
                        showView = [[CollectionShowView alloc]initWithTitle:self.showName];
                        showView.orientation = self.orientation;
                        [showView showInView];
                        
                    } else {
                        [ErrorMessage showErrorMessage:json[@"message"] inView:_tableView];
                    }
                }
            } failure:^(NSError *error) {
                
            }];
        }
        else if (self.type == MBSpaceAddMusicType_Collection) {
            NSString *ValueID = [dic objectForKey:@"ValueID"];
            NSNumber *nums = @3;
            if ([ValueID intValue] >= 1 && [ValueID intValue] <= 4) {
                nums = @1;
            }
            
            [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:SPACE_COLLECTION_COLLECION_URL_PATH,model.UserID,fileId,nums] view:nil success:^(id json) {

                
                if ([json[@"ret_code"] integerValue]==0) {
                    //点赞成功
                    if (showView) {
                        [showView removeFromSuperview];
                        showView = nil;
                    }
                    showView = [[CollectionShowView alloc]initWithTitle:self.showName];
                    showView.orientation = self.orientation;
                    [showView showInView];
                } else {
                    [ErrorMessage showErrorMessage:json[@"message"] inView:self.view];
                }
            } failure:^(NSError *error) {

            }];
        }
        
    }
}


@end
